home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 736 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: mudskipper.cac.psu.edu!user
  2. From: fcusack@tdx.org (frank.)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: beginner question - typecasting
  5. Date: Sat, 06 Jan 1996 13:57:16 -0500
  6. Organization: Penn State University, Center for Academic Computing
  7. Message-ID: <fcusack-0601961357160001@mudskipper.cac.psu.edu>
  8. References: <4cei1r$s02@sun.cis.smu.edu> <30EBCED7.774@sto.fdata.se> <30EDAE8C.582@wildfire.com>
  9. NNTP-Posting-Host: mudskipper.cac.psu.edu
  10.  
  11. In article <30EDAE8C.582@wildfire.com>, Stonewall Ballard
  12. <stoney@wildfire.com> wrote:
  13.  
  14. > Niklas Mellin wrote:
  15. > > 
  16. > > Damon Bowman wrote:
  17. > > >
  18. > > > When you are typecasting, is there any difference between:
  19. > > >
  20. > > > a = int(x)
  21. > > > and
  22. > > > a = (int) x
  23. > > 
  24. > > [...]
  25. > > 
  26. > > Yes the first is not type casting, but a "type conversion".
  27. > > 
  28. > > When x is a  variables of built in types there is no
  29. > > difference. But if x is a varible of some class for instance
  30. > > the first form generates a call to that class' operator int(),
  31. > > generating a compile time error message if there is no such
  32. > > member function.
  33. > > 
  34. > > The 2nd form is an inheritance from plain C, and doesn't work
  35. > > when x is of class type. It is also
  36. > > not "safe", meaning that the compiler will not generate error
  37. > > messages in certain situations, and you will get buggy code
  38. > > or run time errors instead.
  39. > This is not true. The only difference between these forms is the syntax. 
  40. > See section 3.2.5 in Stroustrup's "The C++ Programming Language" 2nd 
  41. > edition.
  42. > Try compiling this:
  43. [...]
  44. > > 
  45. > > In general u should choose the conversion form if possible,
  46. > > but sometimes a type cast can be handy. One case is if you
  47. > > are using a 3rd part class library that is not well written
  48. > > (read MFC) and they forgot to declare function parameters as
  49. > > const and you have a const object you would like to pass to
  50. > > that function. Then you can type cast your object or variable
  51. > > to non const.
  52.  
  53. Let's get some terms straight. The two types of conversions are
  54. _equivalent_, as stated. the form int(x) is called "functional notation"
  55. and the form (int) x is called "cast notation". They are both "type
  56. conversion".
  57.  
  58. The only difference is that if you are converting to a type that is not a
  59. simple type name, you must use cast notation. eg. you cannot use int*(x),
  60. but must use (int *) x. You can typedef int* to another type name however,
  61. to use the functional notation.
  62.  
  63. Again, this is all in Stroustrup 2nd edition, and it's probably in the FAQ.
  64. ~Frank
  65.